const https = require('https'),
    fs = require('fs'),
    http = require('http'),
    axios = require('axios'),
    chreeio = require('cheerio'),
    Iconv = require('iconv-lite'),
    puppeteer = require('puppeteer'),
    request = require('request');

const rootUrl = 'http://www.imomoe.ai/search.asp?page=1&searchword=tv&searchtype=-1';

const { con } = require('../models/common');
// 日志的输出 0最多开发使用,1最少运行使用
let leve = 0;
let rootnum = 1;
class Result {
    constructor() {
        this.id = ' ';//id主键
        this.url = ' ';//目标地址
        this.img = ' ';//图片
        this.name = ' ';//名字
        this.alias = ' ';//别名
        this.type = ' ';//类型+集数
        this.synopsis = ' ';//简介

        this.years = ' ';//年代
        this.region = ' ';// 地区
        this.Indexes = ' '; // 索引
        this.label = ' ';// 标签

        this.num = ' ';//集数
        // this.addnum = ' ';//地址数量
        this.introduction = ' ';// 简介
        this.iteminfo = new Array();
        // 资源路径
        // 第几集的要存到一个表里面把
    }
}

class Vedio {
    constructor() {
        // 所属动漫id
        this.id = ' ';
        // 第几集
        this.num = ' ';
        // 第几个播放链接
        // this.addnum = ' ';
        // 播放地址
        this.url = ' ';
    }
}
let detailurlList = new Array();
let itemList = new Array();
// 获取页数
function getNum(rootUrl) {
    return new Promise((resolve, reject) => {
        request({ encoding: null, url: rootUrl },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    const html = Iconv.decode(body, 'gb2312');
                    let $ = chreeio.load(html);
                    let pageSum = $('#contrainer>.tame>span>em').text();
                    resolve(pageSum);
                }
            });
    })
}

// 定时器操作，防止IP被封
function Timer() {
    return new Promise((resolve, reject) => {
        setTimeout(resolve, 1000);
    })
}
function getIteminfo(url, num,browser) {
    return new Promise((resolve, reject) => {
        request({ encoding: null, url: url },
            async function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    const html = Iconv.decode(body, 'gb2312');
                    let $ = chreeio.load(html);

                    let result = new Result();
                    let url = $('.spay').find('a').attr('href');
                    let info = $('.alex>p').eq(0).text();
                    result.url = url;
                    result.id = parseInt(url.slice(6, 10));
                    result.alias = info;
                    result.img = $('.tpic>img').attr('src');
                    result.synopsis = $('.alex>p').eq(1).text();
                    result.name = $('.tpic>img').attr('alt');
                    result.region = $('.alex').find('span').eq(0).find('a').text();
                    let type = $('.alex').find('span').eq(1).find('a');
                    let typetext = '';
                    for (let i = 0; i < type.length; i++) {
                        typetext += `${$(type[i]).text()} `;
                    }
                    result.type = typetext;
                    result.years = $('.alex').find('span').eq(2).find('a').text();
                    result.label = $('.alex').find('span').eq(3).find('a').first().text();
                    result.Indexes = $('.alex').find('span').eq(4).find('a').text().slice(1, 3);
                    result.num = num;
                    result.introduction = $('body').find('.info').text();

                    itemList.push(result);
                    try {
                        let sql = `select * from animation where id = ${result.id}`;
                        let [rows] = await con.execute(sql);
                        if (rows.length >= 1) {
                            let updata = `update animation set synopsis = '${result.synopsis}' where id = ${result.id}`;
                            let updata2 = `update animation set num = ${result.num} where id = ${result.id}`;
                            await con.execute(updata);
                            await con.execute(updata2);
                            if (leve == 0) {
                                console.log(result.name + '添加成功');
                            }
                        } else {
                            let ins = `insert into animation values('${result.id}','${result.url}','${result.img}','${result.name}','${result.alias}','${result.type}','${result.synopsis}','${result.years}','${result.region}','${result.Indexes}','${result.label}','${result.num}','${result.introduction}',0)`;
                            let [rows2] = await con.execute(ins);
                            if (leve == 0) {
                                console.log(result.name + '添加成功');
                            }
                        }
                    } catch (error) {
                        console.log(error)
                    }

                    if (leve == 0) {
                        console.log(`开始爬取动漫:${result.name}每一集的播放链接。。。`)
                    }
                    let hasnum = `select numhas from animation where id = ${result.id}`;
                    let [rows] = await con.execute(hasnum);
                    if (leve == 0) {
                        console.log('已有集数：' + rows[0].numhas + '资源数：' + result.num);
                    }
                    for (let n = rows[0].numhas; n < result.num; n++) {
                        await Timer();
                        let vedio = new Vedio();
                        let url = `http://www.imomoe.ai/player/${result.id}-0-${n}.html`;
                        console.log(url);
                        vedio.id = result.id;
                        vedio.num = n;
                        vedio.url = await getVideoUrl(browser,url);

                        try {
                            let sql = `select * from vedio where id = ${vedio.id} and num = ${vedio.num}`;
                            let [rows] = await con.execute(sql);
                            let ins = `insert into vedio values('${vedio.id}',${vedio.num},'${vedio.url}')`;
                            let [rows2] = await con.execute(ins);
                            if (leve == 0) {
                                console.log(vedio.id + ' ' + vedio.num + '添加成功');
                            }
                        } catch (error) {
                            console.log(error)
                        }
                    }
                    try {
                        let upnum = `update animation set numhas = ${result.num} where id = ${result.id}`;
                        await con.execute(upnum);
                    } catch (error) {
                        console.log(error)
                    }
                }
                resolve();
            }
        )
    })
}
function getresultNum(url) {
    return new Promise((resolve, reject) => {
        request({ encoding: null, url: url },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    const html = Iconv.decode(body, 'gb2312');
                    let $ = chreeio.load(html);
                    let urlList = $('#play_0>ul').find('li');
                    resolve(urlList.length);
                }
            });
    })
}
// 此函数传入一页url用于得到该页面上的番剧信息
function getItem(url) {
    return new Promise((resolve, reject) => {
        request({ encoding: null, url: url },
            function (error, response, body) {
                if (!error && response.statusCode == 200) {
                    const html = Iconv.decode(body, 'gb2312');
                    let $ = chreeio.load(html);
                    // 链接部分
                    let urlList = $('.fire>.pics>ul').find('li>a');
                    // 在循环中进行爬取到的数据的相关操作
                    // 将页面的url放入队列中
                    // 爬取然后新建一个函数去爬取详情页的信息，传入目标番剧的url在函数中拼接成正确的url
                    // 使用js的队列相关的操作，如果队列不为空则爬取，为空结束当前页面去爬取下一页
                    for (let i = 0; i < urlList.length; i++) {
                        let url = $(urlList[i]).attr('href');
                        detailurlList.push(url);
                    }
                    resolve();
                }
            });
    })
}
async function getVideoUrl(browser,url) {
    try {
        const page = await browser.newPage();
        await page.goto(url);
        let video = await page.$eval('#play2', e => e.src);
        await page.goto(video);
        video = await page.$eval('.dplayer-video-current', e => e.src);
	page.close();
        return video;
    } catch (error) {
        console.log(error);
        return 'timeout';
    }
}
// 将此段代码封装为函数传入不同的url以此达到爬数据的功效
async function Test() {
    console.log('开始');
    const browser = await puppeteer.launch({ args: ['--no-sandbox', '--disable-setuid-sandbox'] });
    let num = await getNum(rootUrl);
    console.log('开始爬取页面。。。' + num);
    for (let i = rootnum; i < rootnum+20; i++) {
        await Timer();
        let url = `http://www.imomoe.ai/search.asp?page=${i}&searchword=tv&searchtype=-1`;
        await getItem(url);
        console.log(`第${i}已经爬取完毕，获取其中的url加入数组。。。`);
    }
    console.log(detailurlList.length);
    console.log(itemList.length);
    console.log('页面爬取完毕，开始爬取各个动漫的详细内容。。。');
    for (let i = 0; i < detailurlList.length; i++) {
        url = `http://www.imomoe.ai${detailurlList[i]}`;
        console.log(url);
        let num = await getresultNum(url);
        await getIteminfo(url, num,browser);
        await Timer();
        console.log(`id为${detailurlList[i]}的动漫爬取完毕`);
    }
    // 打印获取的信息
    if (leve == 5) {
        console.log('开始打印我爬取到的信息。。。');
        for (let i = 0; i < itemList.length; i++) {
            console.log(itemList[i]);
        }
    }
    browser.close();
    console.log('结束');
    process.exit();
}
Test();
module.exports = { Test };

